<template>

  <div class="record-wrapper">
    <div class="record-header-wrapper">
      <div class="record-header-search-wrapper">
        <span class="record-header-search-span">选择课程 : </span>
        <el-select class="record-header-search-selector"
                   v-model="selectedCourseId"
                   @change="onCourseChange"
                   placeholder="请选择课程">
          <el-option
                  v-for="item in courses"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id">
          </el-option>
        </el-select>
      </div>
      <div class="record-header-operation-wrapper">
        <el-button class="record-header-delete-batch-button"
                   type="danger"
                   @click="deleteRecordsByBatch"
                   :disabled="this.multipleSelections.length === 0"
                   icon="el-icon-delete">批量删除</el-button>
      </div>
    </div>
    <div class="record-table-wrapper">
      <el-table
              :data="records"
              v-loading="loading"
              element-loading-text="拼命加载中"
              @selection-change="handleSelectionChange"
              stripe
              style="width: 100%">
        <el-table-column
                type="selection"
                width="55">
        </el-table-column>
        <el-table-column
                prop="course.name"
                label="课程"
                width="200">
        </el-table-column>
        <el-table-column
                prop="studentName"
                label="学生名称"
                width="150">
        </el-table-column>
        <el-table-column
                prop="status"
                label="考勤状态"
                width="120">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.status === '出勤'" type="success" effect="dark">出勤</el-tag>
            <el-tag v-if="scope.row.status === '旷课'" type="danger" effect="dark">旷课</el-tag>
            <el-tag v-if="scope.row.status === '请假'" type="warning" effect="dark">请假</el-tag>
          </template>
        </el-table-column>
        <el-table-column
                prop="recordTime"
                label="考勤时间"
                width="200">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini"
                       @click="showEditRecordDialog(scope.row)"
                       type="primary">编辑</el-button>
            <el-button type="danger"
                       @click="deleteRecord(scope.row)"
                       size="mini">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="record-table-pagination-wrapper">
        <el-pagination
                background
                @current-change="currentChange"
                @size-change="sizeChange"
                layout="sizes, prev, pager, next, jumper, ->, total, slot"
                :total="total">
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
  export default {
    name: "AttdRecord",
    data() {
      return {
        records: [],
        departments: '',
        recordName: '',
        multipleSelections: [],
        loading: false,
        title: '',
        dialogFormVisible: false,
        selectedCourseId: '',
        teacherIdByUser: null,
        total: null,
        size: 10,
        currentPage: 1,
        courses: []
      }
    },
    mounted() {
      this.initTeacherIdByUser()
      this.getRecords()
      this.getCoursesByTeacher()
    },
    methods: {
      //  获取当前操作系统的老师的id
      initTeacherIdByUser() {
        let user = JSON.parse(window.sessionStorage.getItem('user'))
        this.teacherIdByUser = user.teacherId
      },
      //  获取所有考勤记录
      getRecords() {
        this.loading = true
        this.getRequest('/attdmanagement/record/?currentPage=' + this.currentPage
            + '&size=' + this.size
            + '&teacherId=' + this.teacherIdByUser
            + '&courseId=' + this.selectedCourseId).then(resp => {
          if (resp) {
            this.records = resp.data
            this.total = resp.total
            this.loading = false
          }
        })
      },
      //  当分页组件的每页显示多少条数据改变时
      sizeChange(size) {
        this.size = size
        this.currentPage = 1
        this.getRecords()
      },
      //  当分页组件的页数改变时
      currentChange(currentPage) {
        this.currentPage = currentPage
        this.getRecords()
      },
      //  批量删除考勤记录
      deleteRecordsByBatch() {
        this.$confirm('此操作将永久删除这['+ this.multipleSelections.length +']个考勤记录, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let ids = '?'
          this.multipleSelections.forEach(item => {
            ids += 'ids=' + item.id + '&'
          })
          this.deleteRequest('/edumanagement/record/' + ids).then(resp => {
            if (resp) {
              this.getRecords()
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      },
      //  获取被选中的考勤记录
      handleSelectionChange(data) {
        this.multipleSelections = data
      },
      //  展示更新考勤记录dialog
      showEditRecordDialog(data) {
        this.title = '更新考勤记录'
        //  this.updatePos = data 这个直接赋值会导致bug，输入框与table里的数据同步变化
        //  Object.assign(target, source1, source2)方法用于对象的合并，将源对象（source）的所有可枚举属性，复制到目标对象（target)
        Object.assign(this.record, data)
        this.dialogFormVisible = true
      },
      //  删除考勤记录
      deleteRecord(data) {
        this.$confirm('此操作将永久删除['+ data.name +']考勤记录, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.deleteRequest('/edumanagement/record/' + data.id).then(resp => {
            if (resp) {
              this.getRecords()
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      },
      //  添加或更新考勤记录
      dialogRecord() {
        if (this.record.id) {
          //  更新考勤记录
          this.$refs['recordForm'].validate((valid) => {
            if (valid) {
              this.dialogFormVisible = false
              this.putRequest('/edumanagement/record/', this.record).then(resp => {
                if (resp) {
                  this.getRecords()
                }
              })
            }
          })
        } else {
          //  添加考勤记录
          this.$refs['recordForm'].validate((valid) => {
            if (valid) {
              this.dialogFormVisible = false
              this.postRequest('/edumanagement/record/', this.record).then(resp => {
                if (resp) {
                  this.getRecords()
                }
              })
            }
          })
        }
      },
      //  当选择的课程变化时重新获取考勤记录
      onCourseChange() {
        this.loading = true
        this.getRequest('/attdmanagement/record/?currentPage=' + this.currentPage
            + '&size=' + this.size
            + '&teacherId=' + this.teacherIdByUser
            + '&courseId=' + this.selectedCourseId).then(resp => {
          if (resp) {
            this.records = resp.data
            this.total = resp.total
            this.loading = false
          }
        })
      },
      //  通过老师id获取课程
      getCoursesByTeacher() {
        this.getRequest('/attdmanagement/record/course/' + this.teacherIdByUser).then(resp => {
          if (resp) {
            this.courses = resp
          }
        })
      }
    }
  }
</script>

<style lang="less">

  .record-wrapper {
    width: 90%;
    margin-top: 20px;
    margin-left: 40px;

    .record-header-wrapper {
      display: flex;
      justify-content: space-between;
      margin-top: 20px;
      box-sizing: border-box;
      padding: 20px 20px;
      /*background-color: #EBEEF5;*/
      height: 80px;
      box-shadow: 0 2px 4px rgba(0, 0, 0, .20), 0 0 6px rgba(0, 0, 0, .08);
      border-radius: 4px;

      .record-header-search-wrapper {
        display: flex;

        .record-header-search-span {
          margin-right: 10px;
        }

        .record-header-search-selector {
          height: min-content;
        }

      }

      .record-header-operation-wrapper {

        .record-header-add-button {

        }

        .record-header-delete-batch-button {

        }
      }
    }

    .record-table-wrapper {
      margin-top: 20px;
      box-shadow: 0 2px 4px rgba(0, 0, 0, .20), 0 0 6px rgba(0, 0, 0, .08);
      border-radius: 4px;
      padding: 5px 20px 20px 20px;

      .record-table-pagination-wrapper {
        margin-top: 20px;
        display: flex;
        justify-content: center;
      }
    }
  }
</style>